package com.woniu.cms.operation.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.woniu.cms.operation.core.entity.OperationQualitycheck;

import com.woniu.cms.operation.core.service.IOperationQualitycheckService;

import com.woniu.cms.operation.core.vo.OperationBasicinfoVo;
import com.woniu.cms.operation.core.vo.OperationBudgetVo;
import com.woniu.cms.operation.core.vo.OperationQualitycheckVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RCollectionAsync;
import org.redisson.api.RCollectionReactive;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-24
 */
@RestController
@RequestMapping("/operation-qualitycheck")
public class OperationQualitycheckController {



    @Autowired
    private IOperationQualitycheckService iOperationQualitycheckService;

    @ApiOperation("根据id删除质量检查数据")
    @DeleteMapping("/{id}")
    public Result deleteClientCompetitor(@PathVariable Integer id){

        boolean b = iOperationQualitycheckService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除质量检查表")
    @DeleteMapping
    public Result deleteClientCompetitor(@RequestBody List<Integer> ids){
        //批量删除
        boolean b = iOperationQualitycheckService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }



    /**/
    @ApiOperation("修改质量检查表")
    @PutMapping
    public Result updateClientCompetitor(@RequestBody OperationQualitycheck  operationQualitycheck){

        QueryWrapper<OperationQualitycheck> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("qualitycheck_id",operationQualitycheck.getQualitycheckId());

        boolean update = iOperationQualitycheckService.update(operationQualitycheck, queryWrapper);

        return update ? Result.ok() :Result.error();
    }

    @Transactional
    @ApiOperation("保存质量检查表")
    @PostMapping("savecc")
    public Result saveClientCompetitor(@RequestBody OperationQualitycheck  operationQualitycheck){

        boolean save = iOperationQualitycheckService.save(operationQualitycheck);

        OperationQualitycheck operationQualitycheck1 = findIdByOrder(operationQualitycheck.getProjectName());

        return save ? Result.common(operationQualitycheck1) :Result.error();
    }

    @Transactional
    @ApiOperation(value = "通过质量检查表列表项目名称查找质量检查对象")
    @GetMapping("findIdByOrder")
    public  OperationQualitycheck findIdByOrder(String projectName) {

        QueryWrapper<OperationQualitycheck> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_name",projectName);

        queryWrapper.orderByDesc("qualitycheck_id");

        queryWrapper.last("limit 1");

        List<OperationQualitycheck> operationQualitychecks = iOperationQualitycheckService.list(queryWrapper);

        OperationQualitycheck operationQualitycheck1 = operationQualitychecks.get(0);

        return operationQualitycheck1;
    }

    @ApiOperation(value = "通过质量检查表列表Id查找质量检查对象")
    @GetMapping("{id}")
    public  Result findById(@PathVariable   Integer id) {

        OperationQualitycheck operationQualitycheck = iOperationQualitycheckService.getById(id);

        return Result.common(operationQualitycheck);
    }

    @ApiOperation(value = "通过质量检查表中的项目名称查找质量检查表的详细信息")
    @GetMapping("isname")
    public  Result findByName(String projectName) {

        System.out.println("projectName = " + projectName);

        QueryWrapper<OperationQualitycheck> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_name",projectName);

        OperationQualitycheck operationQualitycheck = iOperationQualitycheckService.getOne(queryWrapper);

        return  Result.common(operationQualitycheck);

    }



    @Transactional
    @GetMapping("page")
    @ApiOperation(value = "质量检查表分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageOperationQualitycheck(Integer currentPage, Integer pageSize , String projectName){


        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);
        System.out.println("projectName = " + projectName);

        IPage<OperationQualitycheck> operationQualitycheckIPage = new Page(currentPage,pageSize);

        IPage<OperationQualitycheck> page = iOperationQualitycheckService.page(operationQualitycheckIPage);

        System.out.println("page = " + page);

        List<OperationQualitycheck> records = page.getRecords();

        System.out.println("records = " + records);

        if (projectName!=null){

            records  = records.stream().filter(e -> e.getProjectName() == projectName).collect(Collectors.toList());

        }

        long total = page.getTotal();

        System.out.println("total = " + total);

           OperationQualitycheckVo operationQualitycheckVo = new OperationQualitycheckVo();

        List<OperationQualitycheckVo> operationQualitycheckVos = records.stream().map(e -> {
            OperationQualitycheckVo qualitycheckVo=new OperationQualitycheckVo();
            BeanUtils.copyProperties(e, qualitycheckVo);
            return qualitycheckVo;
        }).collect(Collectors.toList());

        Map<String,Object> map = new HashMap<>();
        map.put("data",operationQualitycheckVos);
        map.put("total",total);

        return Result.common(map);

    }

   /* private List<OperationQualitycheckVo>  copyListProperties(List<OperationQualitycheck>  records){

        List<OperationQualitycheckVo> operationQualitycheckVos  = MyBeanUtils.copyListProperties(records, OperationQualitycheckVo::new);

        return operationQualitycheckVos;

    }*/



}

